#ifndef _PROTOCLHANDLER_H_
#define _PROTOCLHANDLER_H_

#include <StopWatch.h>
#include <shared_data.h>
#include <csp_shared_types.h>
#include <Scheduler.h>
#include <ProcessComposition.h>
#include <LocalStoreManager.h>
#include <libspe2.h>
#include <ChannelManager.h>
#include <Channel.h>

class ProtocolHandler{
public:
	ProtocolHandler(spe_context_ptr_t *ctx, spe_args *spe_setup);
	~ProtocolHandler(){}
	void Talk();
private:
	int ChannelCommunication(uint32_t slot);
	int TransferProcs();
	int Alting(uint32_t slot, int n_guards);
	int TakeProcess(uint32_t slot, uint32_t state);
	int StartProcesses(uint32_t slot);
	void mbox_write(uint32_t sig);
	void read_mbox_blocking(uint32_t*);
	int Poison();

	spe_context_ptr_t *ctx;
	LocalStoreManager *lsManager;
	ea_addr_t *pcb_pointers;
	int spe_id;
	uint64_t *spe_offload_area;
	ProcessComposition *active_procs[MAX_SPU_SLOTS];
	ChannelManager *channelManager;
	Scheduler *scheduler;

	Event *pendingEvent;
	
	int p_swap_out;

	long last_channel_id;
	Channel *last_channel;

	StopWatch sw_transfer_procs;
	StopWatch sw_talk;
};

#endif
